package com.zbw.weightlibrary.WeightSub;

import android.content.Context;
import android.util.Log;

import com.sankuai.hardware.mthwsrvmgrsdk.MTHardwareCenter;
import com.sankuai.hardware.mthwsrvmgrsdk.WeightInfo;
import com.sankuai.hardware.mthwsrvmgrsdk.WeightListener;
import com.zbw.weightlibrary.Connector.IBleControl;
import com.zbw.weightlibrary.Connector.IinfoCallback;

/**
 * 作者 ：Wesley
 * 时间 ：2023-02-13 17:40
 * MeiTuanL4 说明：
 * 美团L4一体秤
 */
public class MeiTuanL4 implements IBleControl {
    private final IinfoCallback mCallback;
    private final Context mContext;
    private boolean stat = true;

    public MeiTuanL4(Context context,IinfoCallback callback) {
        mCallback = callback;
        mContext = context;
        MTHardwareCenter.get().init(mContext, new MTHardwareCenter.OnHSInitCallback() {
            @Override
            public void OnHSInitResult(boolean result) {
                Log.d("初始化结果", result ? "成功" : "失败");
                mCallback.onConnect(result ? "称重秤初始化成功" : "称重秤初始化失败",result);
            }
        });
    }

    private  WeightListener mWeightListener = new WeightListener() {
        @Override
        public void onWeightChange(final WeightInfo weightInfo) {
            if(mCallback != null && stat){
                mCallback.onGetWeight(weightInfo.getNetWeight()+"",weightInfo.isStable());
            }
        }
    };

    /**
     * 设置价格和合计
     * @param unitPrice 价格
     * @param totalPrice 合计
     */
    public static void setUnitAndTotalPrices(float unitPrice, float totalPrice){
        try {
            MTHardwareCenter.get().setUnitAndTotalPrices(unitPrice, totalPrice);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void bleConnect() {
        MTHardwareCenter.get().registerWeightListener(mWeightListener);
    }

    @Override
    public void bleDisconnect() {
        if (mWeightListener != null) {
            MTHardwareCenter.get().unregisterWeightListener(mWeightListener);
        }
    }

    @Override
    public void getWeight() {
        stat = true;
    }

    @Override
    public void stopWeight() {
        stat = false;
    }

    @Override
    public void clearTare() {
        MTHardwareCenter.get().forceZeroing();
    }

    @Override
    public void tare() {
        MTHardwareCenter.get().doTaring();
    }

    @Override
    public void zero() {
        MTHardwareCenter.get().doZeroing();
    }
}
